package com.stable.actor.remotedemo;

import com.stable.actor.core.StableActor;
import com.stable.actor.network.ServiceResponse;
import lombok.extern.slf4j.Slf4j;


@Slf4j
public class InventoryServiceActor extends StableActor {

    public InventoryServiceActor() {
        super();
    }

    @Override
    protected void setupMessageHandlers() {

    }

    @Override
    public void handleMessage(Object message) {
        if (message instanceof InventoryCheckRequest) {
            InventoryCheckRequest request = (InventoryCheckRequest) message;
            System.out.println("📦 库存服务检查: " + request.getProductName() + ", 数量: " + request.getQuantity());

            try {
                Thread.sleep(500); // 模拟处理时间

                // 模拟85%的库存充足率
                boolean success = Math.random() > 0.15;
                String responseMsg = success ?
                        "库存充足 - 商品: " + request.getProductName() + ", 可满足数量: " + request.getQuantity() :
                        "库存不足 - 商品: " + request.getProductName() + ", 请求数量: " + request.getQuantity();

                ServiceResponse response = new ServiceResponse("inventory", success, responseMsg);
                getSender().tell(response, getSelf());

            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                getSender().tell(new ServiceResponse("inventory", false, "库存检查被中断"), getSelf());
            }
        }
    }


    @Override
    protected void preStart() {
        log.info("🚀 库存服务启动完成");
    }
}
